package com.dirk.happie.adapters;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dirk.happie.R;
import com.dirk.happie.activities.MapViewActivity;
import com.dirk.happie.beans.CompanyBean;
import com.dirk.happie.network.DeleteFavoritesTask;
import com.dirk.happie.network.RequestCompleteListener;
import com.dirk.happie.utils.App;
import com.dirk.happie.utils.Availablity;

public class FavoritesListAdapter extends ArrayAdapter<CompanyBean> {
	Activity context;
	int layoutResourceId;
	private DeleteFavoritesTask deleteFavoritesTask = null;
	App mApp = null;

	public FavoritesListAdapter(Activity context, int layoutResourceId, ArrayList<CompanyBean> data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		mApp = (App) context.getApplication();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ProductTypeHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ProductTypeHolder();
			holder.delete = (ImageView) row.findViewById(R.id.imgDelete);
			holder.map = (ImageView) row.findViewById(R.id.btn_map);
			holder.phone = (ImageView) row.findViewById(R.id.btn_phone);
			holder.name = (TextView) row.findViewById(R.id.txt_company);
			holder.address = (TextView) row.findViewById(R.id.txt_company_address);
			holder.phoneNo = (TextView) row.findViewById(R.id.txt_company_phone);

			row.setTag(holder);
		} else {
			holder = (ProductTypeHolder) row.getTag();
			holder.phone = (ImageView) row.findViewById(R.id.btn_phone);
			holder.phone.setVisibility(View.GONE);
		}

		final CompanyBean company = this.getItem(position);
		holder.name.setText(company.getCompanyName());
		holder.address.setText(company.getCompanyAddress());
		if (company.getCompanyPhone() != null && !company.getCompanyPhone().equals("")) {
			holder.phone.setVisibility(View.VISIBLE);
		}
		holder.phoneNo.setText(company.getCompanyPhone());

		holder.map.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, MapViewActivity.class);

				context.startActivityForResult(intent, 10);

			}
		});

		holder.phone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_DIAL);
				String pn = "tel:" + company.getCompanyPhone();
				intent.setData(Uri.parse(pn));
				context.startActivity(intent);

			}
		});

		holder.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!company.getCompanyId().equals(mApp.getDefFavoriteId())) {
					final Dialog dialog = new Dialog(context);
					dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
					dialog.setContentView(R.layout.dialog_sure);
					dialog.setCancelable(true);

					Button yesButton = (Button) dialog.findViewById(R.id.yes_button);
					yesButton.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Availablity.isNetworkAvailable(context)) {
								deleteFavorite(company, mApp.getUserId());
								context.setResult(500);
								dialog.cancel();
							} else {
								Toast.makeText(context, context.getString(R.string.no_connection), Toast.LENGTH_SHORT).show();
							}

						}
					});

					Button noButton = (Button) dialog.findViewById(R.id.no_button);
					noButton.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							dialog.cancel();
						}
					});

					dialog.show();
				} else {
					Toast.makeText(context, context.getString(R.string.sure_favorite), Toast.LENGTH_SHORT).show();
				}
			}
		});

		return row;
	}

	static class ProductTypeHolder {
		ImageView delete;
		ImageView map;
		ImageView phone;
		TextView name;
		TextView address;
		TextView phoneNo;
	}

	private void deleteFavorite(final CompanyBean bean, String user) {
		deleteFavoritesTask = new DeleteFavoritesTask(context);

		deleteFavoritesTask.setRequestCompleteListener(new RequestCompleteListener<String>() {

			@Override
			public void onSuccess(String result) {

				JSONArray jsonArray;
				JSONObject json = null;
				try {
					jsonArray = new JSONArray(result);
					json = jsonArray.getJSONObject(0);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (json != null && json.has("apimsg")) {
					result = json.optString("apimsg", "");
					remove(bean);
					notifyDataSetChanged();

				} else if (json != null && json.has("error")) {
					result = json.optString("error", "");
				}
			}

			@Override
			public void onFailure(final String message) {
			}
		});

		deleteFavoritesTask.execute(user, bean.getCompanyId());

	}
}
